(ns acst.routes.memos
  (:use acst.config.error-config
        acst.config.success-config
        clojure.test
        acst.utils.core)
  (:require [noir.session :as session]
            [noir.validation :as vali]
            [selmer.parser :as parser]
            [compojure.core :refer :all]
            [acst.models.memos :refer :all]
            [acst.models.users :refer [login-require admin-require]]
            [clojure.data.json :as json]
            [acst.models.users :as users]))


(defn get-all-memos [limit page]
  (->
    (let [[int_limit int_page] (parse-to-int  limit page)]
      (find-all-memos int_limit int_page))
    (json/write-str)))

(defn get-memos-by-id [memo_id]
  (->
    (let [[int_memo_id] (parse-to-int memo_id)]
      (find-memo-by-memoid int_memo_id))
    (json/write-str)))

(defn add-memo! [content]
  (login-require
       (->
         (let [user_id (session/get "user_id")]
           (insert-memo! user_id content))                  ;;TODO content要做html验证
         (json/write-str))))

(defn remove-memo! [memo_id]
  (login-require
    (->
      (let [[int_memo_id] (parse-to-int memo_id)]
        (delete-memo! int_memo_id (session/get "user_id")))
      (json/write-str))))

(defn get-comments [memo_id limit page]
  (->
    (let [[int_memo_id int_limit int_page] (parse-to-int memo_id limit page)]
      (cond
        (or (= int_memo_id -1)
            (= int_limit -1)
            (= int_page -1)) (:comment-not-found comments-error)
        :else
        (find-memo-comments int_memo_id int_limit int_page)))
    (json/write-str)))

(defn add-comment! [memo_id content reply_id]
  (login-require
    (->
      (let [user_id (session/get "user_id")
            [int_memo_id int_reply_id] (parse-to-int memo_id reply_id)]
        (println "reply_id" int_reply_id)
        (cond
          (or (= int_memo_id -1)
              (= int_reply_id -1))
          (:can-not-repldy comments-error)
          :else
          (insert-memo-comment! int_memo_id user_id content int_reply_id)))
      (json/write-str))))

(defn get-comments-reply-tree [memo_id comment_id]
  (->
    (let [[int_memo_id int_comment_id] (parse-to-int memo_id comment_id)]
      (cond
        (or (= int_memo_id -1)
            (= int_comment_id -1))
        (:comment-not-found comments-error)
        :else
        (find-memo-comments-reply-tree int_memo_id int_comment_id)))
    (json/write-str)))

(defn remove-memo-comment! [memo_id comment_id]
  (login-require
    (->
      (let [[int_memo_id int_comment_id] (parse-to-int memo_id comment_id)]
        (delete-memo-comment! int_memo_id int_comment_id (session/get "user_id")))
      (json/write-str))))

(defroutes memos-routes
           (context "/api/memos" []
                    (GET  "/" [page] (handler get-all-memos nil 20 page 1))
                    (POST "/" [content] (add-memo!  content))
                    (GET  "/:memo_id" [memo_id] (handler get-memos-by-id memo_id nil))
                    (DELETE "/:memo_id" [memo_id] (handler remove-memo! memo_id nil))
                    (GET  "/:memo_id/comment" [memo_id page] (handler get-comments memo_id nil nil 20 page 1))
                    (POST "/:memo_id/comment" [memo_id content reply_id] (handler add-comment! memo_id nil content nil reply_id nil))
                    (GET  "/:memo_id/comment/:comment_id" [memo_id comment_id] (handler get-comments-reply-tree memo_id nil comment_id nil))
                    (DELETE "/:memo_id/comment/:comment_id" [memo_id comment_id] (handler remove-memo-comment! memo_id nil comment_id nil))))

